package com.gxgy.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gxgy.base.BaseAllInterface;
import com.gxgy.entity.Role;
import com.gxgy.uilt.IfVersionException;


/**
 * @description: 角色 接口
 * @author: 邓 忠柠
 * @date: 2021-11-18
 */
@Controller
public class RoleController extends BaseAllInterface{
	
	@RequestMapping("/addRole")
	public String name() {
		return "page/role/roleAdd";
	}
	
	@RequestMapping("/doAddRole")
	public String addRole(String id, String roleName, String createTime,Model model){
		if (id.isEmpty()) {
			throw new IfVersionException("id不能为空");
		}
		if (roleName.isEmpty()) {
			throw new IfVersionException("角色名称不能为空");
		}
		if (createTime.isEmpty()) {
			throw new IfVersionException("创建时间不能为空");
		}
		Role roleById = roleService.roleById(id);
		if (roleById != null) {
			model.addAttribute("msg","ID已存在");
			return "page/role/roleAdd";
		}
		Role roleByName = roleService.queryRoleByName(roleName);
		if (roleByName != null) {
			model.addAttribute("msg","角色名称已存在");
			return "page/role/roleAdd";
		}
			roleService.addOneRole(id, roleName, createTime);
		
		return "redirect:roleList";
	}
	
	@RequestMapping("/roleList")
	public String queryRole(Model model) {
		List<Role> role = roleService.queryAllRole();
		model.addAttribute("role",role);
		return "page/role/role";
		
	}
	
	@RequestMapping("/deitRole")
	public String deit(String id,Model model) {
		Role role =roleService.roleById(id);
		model.addAttribute("role",role);
		return "page/role/roleEdit";
	}
	
	@RequestMapping("/doEditRole")
	public String doEditRole(String id, String roleName, String createTime,Model model){
		if (id.isEmpty()) {
			model.addAttribute("msg","id不能为空");
			return "page/role/roleEdit";
		}
		if (roleName.isEmpty()) {
			model.addAttribute("msg","角色名称不能为空");
			return "page/role/roleEdit";
		}if (createTime.isEmpty()) {
			model.addAttribute("msg","创建时间不能为空");
			return "page/role/roleEdit";
		}
		Integer role = roleService.updateRole(id, roleName, createTime);
		System.out.println("===================>修改"+role);
		return "redirect:roleList";
		
	}
	
	

}
